<template>
  <PageWrapper :title="pageTitle" :content="content">
    <div class="py-8 bg-white flex flex-col justify-center items-center">
      <BasicForm @register="register" />
      <div class="flex justify-center">
        <a-button @click="resetFields"> 重置 </a-button>
        <a-button class="!ml-4" type="primary" @click="handleSubmit"> 确认 </a-button>
      </div>
    </div>
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { PageWrapper } from '/@/components/Page';
  import { BasicForm, useForm } from '/@/components/Form';
  import { changePassword } from '/@/api/sys/user';
  import { formSchema } from './pwd.data';
  import { useUserStore } from '/@/store/modules/user';
  import { useRouter } from 'vue-router';
  import { PageEnum } from '/@/enums/pageEnum';
  import { message } from 'ant-design-vue';

  export default defineComponent({
    name: 'ChangePassword',
    components: { BasicForm, PageWrapper },
    emits: ['successPwd'],
    props: {
      pageTitle: {
        type: String,
        default: '修改当前用户密码',
      },
      content: {
        type: String,
        default: '修改成功后会自动退出当前登录！',
      },
    },
    setup(props, { emit }) {
      const [register, { validate, resetFields }] = useForm({
        size: 'large',
        baseColProps: { span: 24 },
        labelWidth: 100,
        showActionButtonGroup: false,
        schemas: formSchema,
      });

      const { getInfo, afterLoginAction, logout } = useUserStore();


      async function handleSubmit() {
        try {
          const values = await validate();
          const { passwordNew } = values;

          // TODO custom api
          changePassword({
            pwd: passwordNew,
            account: getInfo.account,
          }).then((res) => {
            if (res.code == 200) {
              message.success('修改成功！');
              emit('successPwd');
            } else {
              message.error(res.message);
            }
          });
        } catch (error) {}
      }

      return { register, resetFields, handleSubmit };
    },
  });
</script>
